package org.come.Jpanel;

import java.awt.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

import com.tool.role.RoleData;
import org.apache.commons.lang.StringUtils;
import org.come.Frame.ChatHistoryJframe;
import org.come.bean.LoginResult;
import org.come.mouslisten.WLLMouslisten;
import org.come.until.GsonUtil;
import org.come.until.ScrenceUntil;
import com.tool.btn.SmallIconBtn;
import com.tool.tcpimg.ChatBox;
import com.tool.tcpimg.InputBean;
import com.tool.tcpimg.UIUtils;

public class FrameMessageChangeJpanel extends JPanel {
	/**
	 * 右侧聊天框面板
	 */

	private SmallIconBtn lableft;// 向左图标
	private SmallIconBtn labbottom;// 向下图标
	private SmallIconBtn labtop;// 向上图标
	//	private SmallIconBtn channel;// 频道选择
//	private SmallIconBtn clear;// 清屏
//	private final SmallIconBtn QieHuan;//切换底图
	//	private final SmallIconBtn locking;//滚动开关
//	private final SmallIconBtn record;//聊天记录
//	private final SmallIconBtn SplitScreen;// 分屏
	private SmallIconBtn lableft2;// 向左图标
	private SmallIconBtn labbottom2;// 向下图标
	private SmallIconBtn labtop2;// 向上图标
	//	private SmallIconBtn channel2;//频道选择
	private SmallIconBtn clear2;// 清屏
	private final SmallIconBtn QieHuan2;//切换底图
	//	private final SmallIconBtn locking2;//滚动开关
//	private final SmallIconBtn record2;//聊天记录
//	private final SmallIconBtn SplitScreen2;// 分屏
	private SmallIconBtn history;
	private static Map<String, String> chatTypes;
	private static boolean[] chatSwitch = new boolean[8];
	private SmallIconBtn chatSwitchBtn;
	public static boolean xianshi=true;
	public static ChatBox chatbox=new ChatBox();
	private static int sum;//记录数
	private static ImageIcon iconS = new ImageIcon("inkImg/old/chat/wxs_" + getSum() + ".png");//聊天框背景;

	public void lingFan(final boolean type) {
		if (type) {
			if (FrameMessageChangeJpanel.sum + 1 < 12) {
				this.lingNumChange(FrameMessageChangeJpanel.sum + 1);
			}
		} else {
			if (FrameMessageChangeJpanel.sum > 0) {
				this.lingNumChange(FrameMessageChangeJpanel.sum - 1);
			}
		}
	}

	static {
		chatbox.setW(ScrenceUntil.ChatFram_X);
		chatbox.setH(ScrenceUntil.ChatFram_y - 20);
	}

	public static ChatBox chatbox1 = new ChatBox();

	static {
		chatbox1.setW(ScrenceUntil.ChatFram_X);
		chatbox1.setH(ScrenceUntil.ChatFram_y / 10 * 5 - 20);
	}

	public static ChatBox chatbox2 = new ChatBox();

	static {
		chatbox2.setW(ScrenceUntil.ChatFram_X);
		chatbox2.setH(ScrenceUntil.ChatFram_y / 10 * 5 - 20);
	}

	public FrameMessageChangeJpanel() {

		//键盘监听
		this.setPreferredSize(new Dimension(ScrenceUntil.ChatFram_X, ScrenceUntil.ChatFram_y));
		this.setLayout(null);
		//  聊天栏上部分---------------------------------------
		// 左按钮   * 0 上  1下  2清屏 3禁言 4左  5调大聊天框  6调小聊天框大小  7透明度改变
		lableft = new SmallIconBtn("inkImg/button/B44.png", 1, 4, "向左", null);
		lableft.setBounds(2, ScrenceUntil.ChatFram_y / 10 * 5 - 17, 17, 17);
		this.add(lableft);

		// 下按钮
		labbottom = new SmallIconBtn("inkImg/button/B43.png", 1, 1, "向下1", null);
		labbottom.setBounds(21, ScrenceUntil.ChatFram_y / 10 * 5 - 17, 17, 17);
		this.add(labbottom);

		// 上按钮
		labtop = new SmallIconBtn("inkImg/button/B42.png", 1, 0, "向上1", null);
		labtop.setBounds(40, ScrenceUntil.ChatFram_y / 10 * 5 - 17, 17, 17);
		this.add(labtop);

//		//频道选择
//		channel = new SmallIconBtn("inkImg/button/B38.png", 1, 2, "频道选择", null, this);
//		channel.setBounds(61, ScrenceUntil.ChatFram_y / 10 * 5 - 17, 17, 17);
//		channel.addMouseListener(new WLLMouslisten(65));
//		this.add(channel);
//
//		// 清屏
//		clear = new SmallIconBtn("inkImg/button/B39.png", 1, 2, "清屏1", null);
//		clear.setBounds(78, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
//		clear.addMouseListener(new WLLMouslisten(66));
//		this.add(clear);
//		//切换底图
//		QieHuan = new SmallIconBtn("inkImg/button/B404.png", 1, 10, "切换底图", null, this);
//		QieHuan.setBounds(97, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
//		QieHuan.addMouseListener(new WLLMouslisten(67));
//		this.add(QieHuan);

//		//滚动开关
//		locking = new SmallIconBtn("inkImg/button/B33.png", 1, 2, "锁定1", null);
//		locking.setBounds(121 - 5, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
//		locking.addMouseListener(new WLLMouslisten(68));
//		this.add(locking);
//
//		//record//聊天记录
//		record = new SmallIconBtn("inkImg/button/B37.png", 1, 2, "清屏1", null);
//		record.setBounds(140 - 5, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
////        record.addMouseListener(new WLLMouslisten(55));
//		this.add(record);
//
//		//SplitScreen;// 分屏
//		SplitScreen = new SmallIconBtn("inkImg/button/B32.png", 1, 2, "清屏1", null);
//		SplitScreen.setBounds(159 - 5, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
////        SplitScreen.addMouseListener(new WLLMouslisten(55));
//		this.add(SplitScreen);
		////////////////////////////////聊天栏下\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
		// 左按钮   * 0 上  1下  2清屏 3禁言 4左  5调大聊天框  6调小聊天框大小  7透明度改变
		lableft2 = new SmallIconBtn("inkImg/button/B44.png", 1, 4, "向左", null);
		lableft2.setBounds(2, ScrenceUntil.ChatFram_y - 18, 17, 17);
		this.add(lableft2);

		// 下按钮
		labbottom2 = new SmallIconBtn("inkImg/button/B43.png", 1, 1, "向下2", null);
		labbottom2.setBounds(21, ScrenceUntil.ChatFram_y - 18, 17, 17);
		this.add(labbottom2);

		// 上按钮
		labtop2 = new SmallIconBtn("inkImg/button/B42.png", 1, 0, "向上2", null);
		labtop2.setBounds(40, ScrenceUntil.ChatFram_y - 18, 17, 17);
		this.add(labtop2);

//		//频道选择
//		channel2 = new SmallIconBtn("inkImg/button/B38.png", 1, 2, "频道选择", null, this);
//		channel2.setBounds(61, ScrenceUntil.ChatFram_y - 18, 17, 17);
//		channel2.addMouseListener(new WLLMouslisten(65));
//		this.add(channel2);

		// 清屏
//		clear2 = new SmallIconBtn("inkImg/button/B39.png", 1, 2, "清屏2", null);
//		clear2.setBounds(78, ScrenceUntil.ChatFram_y - 19, 17, 17);
//		clear2.addMouseListener(new WLLMouslisten(66));
//		this.add(clear2);
		this.clear2 = new SmallIconBtn("inkImg/button/149.png", 1, 0, "记录", (ZhuJpanel)null);
		this.clear2.setBounds(61, ScrenceUntil.ChatFram_y - 19, 17, 17);
		clear2.addMouseListener(new WLLMouslisten(66));
		this.add(this.clear2);


		//切换底图
		QieHuan2 = new SmallIconBtn("inkImg/button/B404.png", 1, 10, "切换底图", null, this);
		QieHuan2.setBounds(78, ScrenceUntil.ChatFram_y / 10 * 5 - 17, 17, 17);
		QieHuan2.addMouseListener(new WLLMouslisten(67));
		this.add(QieHuan2);

		//滚动开关
//		locking2 = new SmallIconBtn("inkImg/button/B33.png", 1, 2, "锁定2", null);
//		locking2.setBounds(122, ScrenceUntil.ChatFram_y / 10 * 5 - 17, 17, 17);
//		locking2.addMouseListener(new WLLMouslisten(68));
//		this.add(locking2);
//
//		//record//聊天记录
//		record2 = new SmallIconBtn("inkImg/button/B37.png", 1, 2, "清屏2", null);
//		record2.setBounds(142, ScrenceUntil.ChatFram_y / 10 * 5 - 17, 17, 17);
//		this.add(record2);
//
//		//SplitScreen;// 分屏
//		SplitScreen2 = new SmallIconBtn("inkImg/button/B32.png", 1, 2, "清屏2", null);
//		SplitScreen2.setBounds(162, ScrenceUntil.ChatFram_y / 10 * 5 - 17, 17, 17);
//		this.add(SplitScreen2);
		chatTypes = new HashMap();
		chatTypes.put("#Q", "当前");
		chatTypes.put("#J", "世界");
		chatTypes.put("#D", "队伍");
		chatTypes.put("#P", "帮派");
		chatTypes.put("#Z", "战斗");
		chatTypes.put("#T", "系统");
		chatTypes.put("#X", "信息");
		chatTypes.put("#S", "传音");
		chatTypes.put("#M","战斗");
		chatTypes.put("#G","传音");
		for (int i = 0; i < chatSwitch.length; i++) {
			chatSwitch[i] = true;
		}

	}


	private ImageIcon icon1 = new ImageIcon("inkImg/old/chat/chatjpanel_tmt.png");//横条

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		// 背景图片
		g.drawImage(iconS.getImage(), 0, 0, ScrenceUntil.ChatFram_X - 3, ScrenceUntil.ChatFram_y, this);
		g.drawImage(this.icon1.getImage(), 0, ScrenceUntil.ChatFram_y / 10 * 5 - 20, ScrenceUntil.ChatFram_X - 3, 20, this);
		g.drawImage(this.icon1.getImage(), 0, ScrenceUntil.ChatFram_y - 20, ScrenceUntil.ChatFram_X - 3, 20, this);
		Graphics g22 = g.create(5, 0, ScrenceUntil.ChatFram_X - 5, ScrenceUntil.ChatFram_y / 10 * 5 - 20);
		g.translate(3, 0);
		chatbox1.paint(g22);
		g22.dispose();
		Graphics g23 = g.create(5, ScrenceUntil.ChatFram_y / 10 * 5, ScrenceUntil.ChatFram_X - 5, ScrenceUntil.ChatFram_y / 10 * 5 - 20);
		chatbox2.paint(g23);
		g23.dispose();


	}

	/**
	 * 添加聊天信息
	 * 0当前 1队伍 2帮派 3世界 4传音 5系统 6消息
	 *
	 * @return
	 */
	public static void addtext(int type, String text, BigDecimal id, String name) {
		addtext(type,text,id,name,null);
	}
	public static void addtext(int type, String text, BigDecimal id, String name, BigDecimal liangId) {
		//0当前 1队伍 2帮派 3世界 4传音 5系统 6消息 7系统加传音
		StringBuffer buffer = new StringBuffer();
		InputBean inputBean;
		if (type == 4) {
			if (id != null) {
				inputBean = new InputBean();
				inputBean.setType(1);
				inputBean.setId(id);
				inputBean.setLiangId(liangId);
				inputBean.setName("[" + name + "]");
				inputBean.setColor("cFFFF00");
				buffer.append("#V");
				buffer.append(GsonUtil.getGsonUtil().getgson().toJson(inputBean));
				buffer.append("#L#W");
			}

			buffer.append(text);
			GameJpanel.getGameJpanel().addNotice(buffer.toString());
		} else {
			if (type == 7) {
				GameJpanel.getGameJpanel().addBox("#T" + text, 8, null);
			}

			if (type == 0) {
				buffer.append("#Q");
			} else if (type == 1) {
				buffer.append("#D");
			} else if (type == 2) {
				buffer.append("#P");
			} else if (type == 3) {
				buffer.append("#J");
			} else if (type != 5 && type != 7) {
				if (type == 6) {
					buffer.append("#X");
				} else if (type==11){
					buffer.append("#M");
				}
				//#T 系统* #X 信息* #J 世界* #Q 当前* #D 队伍* #P 帮派
			}  else {
				buffer.append("#T");
			}

			if (type != 5 && type != 6 && type != 7) {
				if (id != null) {
					inputBean = new InputBean();
					inputBean.setType(1);
					inputBean.setId(id);
					inputBean.setLiangId(liangId);
					inputBean.setName("[" + name + "]");
					inputBean.setColor("G");
					buffer.append("#V");
					buffer.append(GsonUtil.getGsonUtil().getgson().toJson(inputBean));
					buffer.append("#L");
				} else if (name != null) {
					buffer.append("[" + name + "]");
				}

				buffer.append(text);
			} else {
				buffer.append("#Y");
				buffer.append(text);
			}
			if (type == 5 | type == 6) {
				chatbox1.addtext(buffer.toString(), ScrenceUntil.ChatFram_X - 20, UIUtils.TEXT_FONT1);
			} else {
				chatbox2.addtext(buffer.toString(), ScrenceUntil.ChatFram_X - 20, UIUtils.TEXT_FONT1);
			}
			if (chatbox.isDisplay()) {
				chatbox.addtext(buffer.toString(), ScrenceUntil.ChatFram_X - 20, UIUtils.TEXT_FONT1);
			}
		}
		addChatHistory(buffer.toString());
	}
	/**
	 * 添加聊天信息
	 * 0当前 1队伍 2帮派 3世界 4传音 5系统 6消息
	 * @return
	 */
	public static void addtext(int type,String text,BigDecimal id,String name,String goodNum,Integer goodNumType) {
		int switchType = type;
		switch (switchType) {
			case 4:
				switchType = -1;
				break; // 不需要控制
			case 7:
			case 8:
			case 9:
			case 10:
				switchType = 5;
				break;
			case 11:
				switchType = 4;
				break;
		}

		//0当前 1队伍 2帮派 3世界 4传音 5系统 6消息 7系统加传音
		StringBuffer buffer = new StringBuffer();
		if (type == 4) {
			if (id != null) {
				final InputBean inputBean = new InputBean();
				inputBean.setType(1);
				inputBean.setId(id);
				buffer.append("#S#G");
				if (StringUtils.isNotBlank(goodNum)) {
					inputBean.setGoodNum(goodNum);
					inputBean.setGoodNumType(String.valueOf(goodNumType));
				}
				if (StringUtils.isNotBlank(inputBean.getGoodNum())) {
					inputBean.setName("[" + name);
				} else {
					inputBean.setName("[" + name + "]");
				}
				inputBean.setColor("cFFFF00");
				buffer.append("#V");
				buffer.append(GsonUtil.getGsonUtil().getgson().toJson(inputBean));
				buffer.append("#L#Y");
			}
			buffer.append(text);
			GameJpanel.getGameJpanel().addNotice(buffer.toString());
		} else {
			if (type == 7) {
//			GameJpanel.getGameJpanel().addNotice("#T"+text);
				GameJpanel.getGameJpanel().addBox("#T" + text, 8, null);
			}
			if (type == 0) {
				buffer.append("#Q");
			} else if (type == 1) {
				buffer.append("#D");
			} else if (type == 2) {
				buffer.append("#P");
			} else if (type == 3) {
				buffer.append("#J");
			} else if (type == 5 || type == 7) {
				buffer.append("#T");
			} else if (type == 6) {
				buffer.append("#X");
			}
			//#T 系统* #X 信息* #J 世界* #Q 当前* #D 队伍* #P 帮派
			if (type == 5 || type == 7) {
				buffer.append("#Y");
				buffer.append(text);
			} else if (type == 6) {
				buffer.append(text);
			} else {
				if (id != null) {
					InputBean inputBean = new InputBean();
					inputBean.setType(1);
					inputBean.setId(id);
					final LoginResult login = RoleData.getRoleData().getLoginResult();
					if (StringUtils.isNotBlank(goodNum)) {
						inputBean.setGoodNum(goodNum);
						inputBean.setGoodNumType(String.valueOf(goodNumType));
					}
					if (StringUtils.isNotBlank(inputBean.getGoodNum())) {
						inputBean.setName("[" + name);
					} else {
						inputBean.setName("[" + name + "]");
					}
					inputBean.setColor("G");
					buffer.append("#V");
					buffer.append(GsonUtil.getGsonUtil().getgson().toJson(inputBean));
					buffer.append("#L");
				} else if (name != null) {
					buffer.append("[" + name + "]");
				}
				buffer.append(text);
			}
		}
		if (switchType == -1 || chatSwitch[switchType]) {
			if (FrameMessageChangeJpanel.chatbox.isDisplay()) {
				chatbox.addtext(buffer.toString(), ScrenceUntil.ChatFram_X - 20, UIUtils.TEXT_FONT1);
			} else {
				if (type == 6 || type == 4 || type == 5) {//修改显示聊天框上面
					chatbox1.addtext(buffer.toString(), ScrenceUntil.ChatFram_X - 20, UIUtils.TEXT_FONT1);
				} else {//修改显示聊天框下面
					chatbox2.addtext(buffer.toString(), ScrenceUntil.ChatFram_X - 20, UIUtils.TEXT_FONT1);
				}
			}
		}
		addChatHistory(buffer.toString());
	}
	/**
	 * 按钮位置切换
	 */
	public void BtnChange() {
		lableft.setBounds(2, ScrenceUntil.ChatFram_y / 10 * 5 - 20, 20, 20);
		labbottom.setBounds(21, ScrenceUntil.ChatFram_y / 10 * 5 - 20, 20, 20);
		labtop.setBounds(40, ScrenceUntil.ChatFram_y / 10 * 5 - 20, 20, 20);
//		channel.setBounds(59, ScrenceUntil.ChatFram_y / 10 * 5 - 20, 20, 20);
//		clear.setBounds(78, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
//		QieHuan.setBounds(97, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
//		locking.setBounds(121 - 5, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
//		record.setBounds(140 - 5, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
//		SplitScreen.setBounds(159 - 5, ScrenceUntil.ChatFram_y / 10 * 5 - 18, 17, 17);
		if (!chatbox1.isDisplay()) {
			chatbox1.setW(ScrenceUntil.ChatFram_X);
			chatbox1.setH(ScrenceUntil.ChatFram_y / 10 * 5 - 20);
		}
		lableft2.setBounds(2, ScrenceUntil.ChatFram_y - 21, 20, 20);
		labbottom2.setBounds(21, ScrenceUntil.ChatFram_y - 21, 20, 20);
		labtop2.setBounds(40, ScrenceUntil.ChatFram_y - 21, 20, 20);
//		channel2.setBounds(59, ScrenceUntil.ChatFram_y - 21, 20, 20);
		clear2.setBounds(59, ScrenceUntil.ChatFram_y - 19, 17, 17);
		QieHuan2.setBounds(78, ScrenceUntil.ChatFram_y - 19, 17, 17);
//		locking2.setBounds(121 - 5, ScrenceUntil.ChatFram_y - 19, 17, 17);
//		record2.setBounds(140 - 5, ScrenceUntil.ChatFram_y - 19, 17, 17);
//		SplitScreen2.setBounds(159 - 5, ScrenceUntil.ChatFram_y - 19, 17, 17);
		if (!chatbox2.isDisplay()) {
			chatbox2.setW(ScrenceUntil.ChatFram_X);
			chatbox2.setH(ScrenceUntil.ChatFram_y / 10 * 5 - 20);
		}
	}

	public SmallIconBtn getLableft() {
		return lableft;
	}

	public void setLableft(SmallIconBtn lableft) {
		this.lableft = lableft;
	}

	public SmallIconBtn getLabbottom() {
		return labbottom;
	}

	public void setLabbottom(SmallIconBtn labbottom) {
		this.labbottom = labbottom;
	}

	public SmallIconBtn getLabtop() {
		return labtop;
	}

	public void setLabtop(SmallIconBtn labtop) {
		this.labtop = labtop;
	}

	public SmallIconBtn getLableft2() {
		return lableft2;
	}

	public void setLableft2(SmallIconBtn lableft2) {
		this.lableft2 = lableft2;
	}

	public SmallIconBtn getLabbottom2() {
		return labbottom2;
	}

	public void setLabbottom2(SmallIconBtn labbottom2) {
		this.labbottom2 = labbottom2;
	}

	public SmallIconBtn getLabtop2() {
		return labtop2;
	}

	public void setLabtop2(SmallIconBtn labtop2) {
		this.labtop2 = labtop2;
	}

	public static int getSum() {
		return FrameMessageChangeJpanel.sum;
	}

	public static void setSum(final int sum) {
		FrameMessageChangeJpanel.sum = sum;
	}

	public void lingNumChange(final int number) {
		FrameMessageChangeJpanel.sum = number;
	}

	public static ImageIcon getIconH() {
		return iconS;
	}

	public static void setIconH(ImageIcon iconS) {
		FrameMessageChangeJpanel.iconS = iconS;
	}

//	public SmallIconBtn getChannel() {
//		return channel;
//	}
//
//	public void setChannel(SmallIconBtn channel) {
//		this.channel = channel;
//	}
//
//	public SmallIconBtn getClear() {
//		return clear;
//	}
//
//	public void setClear(SmallIconBtn clear) {
//		this.clear = clear;
//	}

//	public SmallIconBtn getChannel2() {
//		return channel2;
//	}

//	public void setChannel2(SmallIconBtn channel2) {
//		this.channel2 = channel2;
//	}

	public SmallIconBtn getClear2() {
		return clear2;
	}

	public void setClear2(SmallIconBtn clear2) {
		this.clear2 = clear2;
	}

	public static ChatBox getChatbox() {
		return chatbox;
	}

	public static void setChatbox(ChatBox chatbox) {
		FrameMessageChangeJpanel.chatbox = chatbox;
	}

	public static ChatBox getChatbox1() {
		return chatbox1;
	}

	public static void setChatbox1(ChatBox chatbox1) {
		FrameMessageChangeJpanel.chatbox1 = chatbox1;
	}

	public static ChatBox getChatbox2() {
		return chatbox2;
	}

	public static void setChatbox2(ChatBox chatbox2) {
		FrameMessageChangeJpanel.chatbox2 = chatbox2;
	}

	public ImageIcon getIcon1() {
		return icon1;
	}

	public void setIcon1(ImageIcon icon1) {
		this.icon1 = icon1;
	}
	public static boolean getChatSwitch(int index) {
		return chatSwitch[index];
	}

	public static void setChatSwitch(int index, boolean is) {
		chatSwitch[index] = is;
	}

	public static boolean[] getChatSwitch() {
		return chatSwitch;
	}
	private static synchronized void addChatHistory(String msg) {
		String chatType = msg.substring(0, 2);
		ChatHistoryJframe.getChatHistoryJframe().getChatHistoryJpanel().addChatHistory((String)chatTypes.get(chatType), msg);
	}
}
